// pages/headlinesDetail/headlinesDetail.js
import api from '../../utils/api.js'
var app = getApp();
const util = require('../../utils/fenxiang.js');
import { HTTP } from '../../utils/http.js'
import { config } from '../../utils/config'
let http = new HTTP()
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        infoId: -1, // 头条id
        info: "", // 头条资讯详情
        sureLikeInformation: false, // 用户是否点赞一片文章
        sureCollectInformation: false, // 用户是否收藏一片文章
        size: 100, // 留言一页展示的条数
        infoList: [], // 留言列表
        content: "", // 评论输入框里面的内容
        zanNum: 1425,
        imgUrl: config.imgUrl,
        createTime: "" // 创建时间
    },

    // 接口
    // 拿到单个头条资讯详情的接口
    getOneInformation(obj) {
        http.request({
            url: api.getOneInforM,
            data: obj,
            success: res => {
                console.log(res.data[0])
                var info = res.data[0]
                var createTime = info.create_time
                createTime = createTime.split(" ")[0]
                this.setData({
                    info,
                    // content:info.content.replace(/\<img/g,'<img style="display:block;width:100%height:auto;"')
                    createTime
                })
                WxParse.wxParse('article', 'html', info.content, this, 5);
            }
        })
    },
    //判读用户是否点赞一片文章的接口
    sureLikeInformation(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.sureLikeInforMation,
                data: obj,
                success: res => {
                    console.log(res)
                    var sureLikeInformation = true
                    if (res.message == "该用户还没有点赞这条文章") {
                        sureLikeInformation = false
                    }
                    this.setData({
                        sureLikeInformation
                    })
                    resolve(res.data);
                }
            })
        })
    },
    //用户点赞或取消点赞一篇资讯文章的接口
    addDelLikeInformation(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelLikeInforM,
                data: obj,
                success: res => {
                    console.log(res)
                    if (res.message == "取消成功啦") {
                        wx.showToast({
                            title: '取消成功',
                            duration: 1500
                        })
                    } else {
                        wx.showToast({
                            title: '点赞成功',
                            duration: 1500
                        })
                    }
                    resolve(res.data);
                }
            })
        })
    },
    //判断用户是否收藏一篇文章的接口
    sureCollectInformation(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.sureCollectInforM,
                data: obj,
                success: res => {
                    var sureCollectInformation = true
                    if (res.message == "该用户还没有收藏这条文章") {
                        sureCollectInformation = false
                    }
                    this.setData({
                        sureCollectInformation
                    })
                    resolve(res.data);
                }
            })
        })
    },
    //用户收藏一篇文章的接口
    addDelCollectInformation(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelCollectInforM,
                data: obj,
                success: res => {
                    console.log(res)
                    if (res.message == "取消收藏成功啦") {
                        wx.showToast({
                            title: '取消成功',
                            duration: 1500
                        })
                    } else {
                        wx.showToast({
                            title: '收藏成功',
                            duration: 1500
                        })
                    }
                    resolve(res.data);
                }
            })
        })
    },
    //获得一个文章下所有在留言以及子留言的接口
    getAllInfoComment(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.getAllInfoComment,
                data: obj,
                success: res => {
                    resolve(res.data);
                }
            })
        })
    },
    //判断用户是否点赞一条文章留言的接口
    sureInfoComment(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.sureInfoComment,
                data: obj,
                success: res => {
                    resolve(res);
                }
            })
        })
    },
    //对文章添加一条留言的接口
    addInformationInfo(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addInforMationInfo,
                data: obj,
                success: res => {
                    console.log(res)
                    if (res.message = "添加留言成功") {
                        wx.showToast({
                            title: '评论成功',
                            duration: 1500
                        })
                        var infoList = this.data.infoList
                        var info = {
                            attention: 0,
                            classes: res.data.classes,
                            client_id: res.data.client_id,
                            content: res.data.content,
                            create_time: res.data.create_time,
                            id: res.data.id,
                            parent_id: [],
                            is_check: 0,
                            project_id: res.data.project_id,
                            users: {
                                id: wx.getStorageSync("user_id"),
                                clientavatar: wx.getStorageSync("avatarUrl"),
                                nickname: wx.getStorageSync("nickName")
                            },
                        }
                        infoList.unshift(info)
                        this.setData({
                            content: "",
                            infoList
                        })
                    }
                    resolve(res.data);
                }
            })
        })
    },
    //用户对文章留言的点赞与取消点赞功能
    addDelAttentionInfo(obj) {
        return new Promise((resolve, reject) => {
            http.request({
                url: api.addDelAttentionInfo,
                data: obj,
                success: res => {
                    console.log(res)
                    resolve(res.data);
                }
            })
        })
    },


    // 用户点赞一片文章
    zan() {
        var sureLikeInformation = this.data.sureLikeInformation
        var info = this.data.info
        info.like_num = info.like_num + 1
        var status_id = 1
        if (sureLikeInformation) {
            status_id = 2
            info.like_num = info.like_num - 2
        }
        sureLikeInformation = !sureLikeInformation
        this.setData({
            sureLikeInformation,
            info
        })

        this.addDelLikeInformation({ user_id: wx.getStorageSync("user_id"), information_id: this.data.infoId, status_id })
    },
    // 用户收藏一片文章
    collection() {
        var sureCollectInformation = this.data.sureCollectInformation
        var status_id = 1
        if (sureCollectInformation) {
            status_id = 2
        }
        sureCollectInformation = !sureCollectInformation
        this.setData({
            sureCollectInformation
        })
        this.addDelCollectInformation({ user_id: wx.getStorageSync("user_id"), information_id: this.data.infoId, status_id })
    },
    // 获取评论输入框里面的内容
    write(e) {
        this.setData({
            content: e.detail.value
        })
    },
    // 评论输入框获取焦点
    focus(e) {
        this.setData({
            flag: true,
        })
    },
    // 评论输入框失去焦点
    blur() {
        setTimeout(() => {
            this.setData({
                flag: false,
                content: "",
                focus: false,
            })
        }, 300)
    },
    // 回车发送评论
    send() {
        console.log(this.data.focus)
        if (!this.data.content) {
            return false
        }
        let obj = {
            parent_id: 0,
            content: this.data.content,
            client_id: wx.getStorageSync("user_id"),
            information_id: this.data.infoId,
            classes: 0
        }
        this.addInformationInfo(obj)
    },
    // 给评论点赞
    like(e) {
        let id = e.currentTarget.dataset.id
        console.log(id)
        let infoList = this.data.infoList
        let info = infoList.filter(item => {
            return item.id == id
        })[0]
        let statusID = 1
        if (!info.is_check) {
            info.is_check = 1
            info.attention++
        } else {
            info.is_check = 0
            info.attention--
                statusID = 2
        }
        let obj = {
            id,
            information_id: this.data.infoId,
            uid: wx.getStorageSync("user_id"),
            statusID
        }
        this.addDelAttentionInfo(obj)
        this.setData({
            infoList
        })
    },
    enter() {
        wx.navigateTo({
            url: '/pages/project/project?id=' + this.data.info.project_id,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var id = options.id || 32
        this.setData({
            infoId: id
        })
        this.getOneInformation({ id }) //获取头条的详情
        this.sureLikeInformation({ user_id: wx.getStorageSync("user_id"), information_id: id }) //用户是否点赞一片文章
        this.sureCollectInformation({ user_id: wx.getStorageSync("user_id"), information_id: id }) //用户是否收藏一片文章
        this.getAllInfoComment({ id, page: 1, size: this.data.size }).then(res => {
            if (res) {
                var infoList = res
                infoList.map((item, index) => {
                    this.sureInfoComment({ id: item.id, uid: wx.getStorageSync("user_id") }).then(res => {
                        if (res.message == "该用户以点赞这条留言") {
                            item.is_check = 1
                        }
                        if (index == infoList.length - 1) {
                            this.setData({
                                infoList
                            })
                        }
                    })
                })
                this.setData({
                    infoList
                })
            } else {
                this.setData({
                    infoList: []
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    // 分享按钮
    onShareAppMessage: function(res) {
        console.log(this.data.infoId)
        return {
            path: `/pages/index/index?redirect=/pages/headlinesDetail/headlinesDetail&id=${this.data.infoId}&typeShare=1`
        }
    }
})